package com.csx.governance.traffic.adm.controller;

import com.csx.governance.traffic.adm.model.bo.RegistryCenterInfoBO;
import com.csx.governance.traffic.adm.model.bo.ServiceInfoBO;
import com.csx.governance.traffic.adm.model.common.Result;
import com.csx.governance.traffic.adm.model.dto.RegistryCenterInfoDTO;
import com.csx.governance.traffic.adm.model.dto.ServiceInfoDTO;
import com.csx.governance.traffic.adm.model.vo.ImportResultVO;
import com.csx.governance.traffic.adm.model.vo.RegistryCenterInfoVO;
import com.csx.governance.traffic.adm.model.vo.ServiceInfoVO;
import com.csx.governance.traffic.adm.service.ServiceDiscoveryService;
import com.csx.governance.traffic.adm.util.ModelConvertUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author cuisongxu
 * @date 2023/4/24 16:53
 */
@Tag(name = "服务发现控制器")
@RequestMapping("/service-discovery")
@RestController
@AllArgsConstructor
public class ServiceDiscoveryController {

    @Autowired
    private ServiceDiscoveryService serviceDiscoveryService;

    /**
     * 获取服务列表
     * @return 服务列表
     */
    @Operation(summary = "获取服务列表")
    @GetMapping("/services")
    public Result<List<ServiceInfoVO>> getServices() {
        List<ServiceInfoDTO> services = serviceDiscoveryService.getServices();
        List<ServiceInfoVO> res = ModelConvertUtil.convert(services, ServiceInfoVO.class);
        return Result.success(res);
    }

    /**
     * 根据服务名获取服务
     * @param serviceName 服务名
     * @return 服务信息
     */
    @Operation(summary = "根据服务名获取服务")
    @GetMapping("/service")
    public Result<ServiceInfoVO> getService(@RequestParam("serviceName") @Parameter(description = "要获取服务信息的服务名") String serviceName) {
        ServiceInfoDTO service = serviceDiscoveryService.getService(serviceName);
        ServiceInfoVO res = ModelConvertUtil.convert(service, ServiceInfoVO.class);
        return Result.success(res);
    }
    /**
     * 新增服务
     * @param serviceInfoDTO 新增的服务信息传输实体
     */
    @Operation(summary = "新增服务")
    @PostMapping("/add-service")
    public Result<Void> addService(@RequestBody @Parameter(description = "服务信息传输实体") ServiceInfoDTO serviceInfoDTO) {
        ServiceInfoBO serviceInfoBO = ModelConvertUtil.convert(serviceInfoDTO, ServiceInfoBO.class);
        serviceDiscoveryService.addService(serviceInfoBO);
        return Result.success();
    }

    /**
     * 修改服务
     * @param serviceInfoDTO 修改的服务信息的传输实体
     */
    @Operation(summary = "更新服务")
    @PostMapping("/update-service")
    public Result<Void> updateService(@RequestBody @Parameter(description = "服务信息传输实体") ServiceInfoDTO serviceInfoDTO) {
        ServiceInfoBO serviceInfoBO = ModelConvertUtil.convert(serviceInfoDTO, ServiceInfoBO.class);
        serviceDiscoveryService.updateService(serviceInfoBO);
        return Result.success();
    }

    /**
     * 删除服务
     * @param serviceNames 需要删除的服务名列表
     */
    @Operation(summary = "删除服务")
    @PostMapping("/delete-service")
    public Result<Void> deleteServices(@RequestBody @Parameter(description = "期望删除的服务的服务名列表") List<String> serviceNames) {
        serviceDiscoveryService.deleteServices(serviceNames);
        return Result.success();
    }

    /**
     * 下载模板
     * @param response 响应对象
     */
    @Operation(summary = "下载模板")
    @PostMapping("/download-template")
    public void downloadTemplate(HttpServletResponse response) {
        serviceDiscoveryService.downloadTemplate(response);
    }

    /**
     * 导出服务列表
     * @param serviceNames 服务名列表
     * @param response 响应对象
     */
    @Operation(summary = "导出服务")
    @PostMapping("/export-services")
    public void exportServices(@RequestBody @Parameter(description = "要导出的服务的服务名列表") List<String> serviceNames, HttpServletResponse response) {
        serviceDiscoveryService.exportServices(serviceNames, response);
    }

    /**
     * 导入服务列表
     * @param file 文件
     * @param repeatStrategyCode 重复处理策略码: 0: 跳过; 1 覆盖; 2 终止
     * @return
     */
    @Operation(summary = "导入服务")
    @PostMapping("/import-services")
    public Result<ImportResultVO<List<String>>> importServices(@RequestParam("file") @Parameter(description = "文件") MultipartFile file,
                                                @RequestParam("repeatStrategyCode") @Parameter(description = "重复处理策略码: 0: 跳过; 1 覆盖; 2 终止") Integer repeatStrategyCode) throws IOException {

        ImportResultVO<List<String>> importResultVO = serviceDiscoveryService.importServices(new String(file.getBytes()), repeatStrategyCode);
        return Result.success(importResultVO);
    }

    /**
     * 获取注册中心列表
     * @return 注册中心列表
     */
    @Operation(summary = "获取注册中心列表")
    @GetMapping("/registryCenters")
    public Result<List<RegistryCenterInfoVO>> getRegistryCenters() {
        List<RegistryCenterInfoDTO> registryCenters = serviceDiscoveryService.getRegistryCenters();
        List<RegistryCenterInfoVO> res = ModelConvertUtil.convert(registryCenters, RegistryCenterInfoVO.class);
        return Result.success(res);
    }

    /**
     * 根据ID获取注册中心信息
     * @param id 注册中心ID
     * @return 注册中心信息
     */
    @Operation(summary = "根据ID获取注册中心")
    @GetMapping("/registryCenter")
    public Result<RegistryCenterInfoVO> getRegistryCenter(@RequestParam("id") @Parameter(description = "注册中心ID") String id) {
        RegistryCenterInfoDTO registryCenter = serviceDiscoveryService.getRegistryCenter(id);
        RegistryCenterInfoVO res = ModelConvertUtil.convert(registryCenter, RegistryCenterInfoVO.class);
        return Result.success(res);
    }


    /**
     * 新增注册中心
     * @param registryCenterInfoDTO 新增的注册中心传输实体
     */
    @Operation(summary = "新增注册中心")
    @GetMapping("/add-registryCenter")
    public Result<Void> addRegistryCenter(@RequestBody @Parameter(description = "注册中心传输实体") RegistryCenterInfoDTO registryCenterInfoDTO) {
        RegistryCenterInfoBO registryCenterInfoBO = ModelConvertUtil.convert(registryCenterInfoDTO, RegistryCenterInfoBO.class);
        serviceDiscoveryService.addRegistryCenter(registryCenterInfoBO);
        return Result.success();
    }

    /**
     * 修改注册中心
     * @param registryCenterInfoDTO 修改的注册中心传输实体
     */
    @Operation(summary = "修改注册中心")
    @GetMapping("/update-registryCenter")
    public Result<Void> updateRegistryCenter(@RequestBody @Parameter(description = "注册中心传输实体") RegistryCenterInfoDTO registryCenterInfoDTO) {
        RegistryCenterInfoBO registryCenterInfoBO = ModelConvertUtil.convert(registryCenterInfoDTO, RegistryCenterInfoBO.class);
        serviceDiscoveryService.updateRegistryCenter(registryCenterInfoBO);
        return Result.success();
    }

    /**
     * 删除注册中心
     * @param ids 待删除的注册中心的ID列表
     */
    @Operation(summary = "删除注册中心")
    @GetMapping("/delete-registryCenters")
    public Result<Void> deleteRegistryCenters(@RequestBody @Parameter(description = "待删除的注册中心ID列表") List<String> ids) {
        serviceDiscoveryService.deleteRegistryCenters(ids);
        return Result.success();
    }

}
